package example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 多数据库连接池管理器
 */
public class DatabaseConnectionMultiton {
    private static final Map<String, DatabaseConnectionMultiton> instances = new ConcurrentHashMap<>();
    private static final int MAX_POOL_SIZE = 10;
    
    private final String databaseName;
    private final Connection[] connectionPool;
    private final boolean[] usedConnections;
    private int availableConnections;
    
    private DatabaseConnectionMultiton(String databaseName) {
        this.databaseName = databaseName;
        this.connectionPool = new Connection[MAX_POOL_SIZE];
        this.usedConnections = new boolean[MAX_POOL_SIZE];
        this.availableConnections = MAX_POOL_SIZE;
        
        initializeConnectionPool();
    }
    
    public static synchronized DatabaseConnectionMultiton getInstance(String databaseName) {
        return instances.computeIfAbsent(databaseName, k -> new DatabaseConnectionMultiton(k));
    }
    
    private void initializeConnectionPool() {
        try {
            String url = "jdbc:mysql://localhost:3306/" + databaseName;
            String username = "root";
            String password = "password";
            
            for (int i = 0; i < MAX_POOL_SIZE; i++) {
                connectionPool[i] = DriverManager.getConnection(url, username, password);
                usedConnections[i] = false;
            }
            System.out.println("初始化数据库连接池: " + databaseName + ", 连接数: " + MAX_POOL_SIZE);
        } catch (SQLException e) {
            throw new RuntimeException("初始化数据库连接池失败: " + databaseName, e);
        }
    }
    
    public synchronized Connection getConnection() throws InterruptedException {
        while (availableConnections == 0) {
            wait();
        }
        
        for (int i = 0; i < MAX_POOL_SIZE; i++) {
            if (!usedConnections[i]) {
                usedConnections[i] = true;
                availableConnections--;
                System.out.println("获取数据库连接: " + databaseName + ", 索引: " + i);
                return connectionPool[i];
            }
        }
        
        return null; // 理论上不会执行到这里
    }
    
    public synchronized void releaseConnection(Connection connection) {
        for (int i = 0; i < MAX_POOL_SIZE; i++) {
            if (connectionPool[i] == connection) {
                usedConnections[i] = false;
                availableConnections++;
                notifyAll();
                System.out.println("释放数据库连接: " + databaseName + ", 索引: " + i);
                break;
            }
        }
    }
    
    public String getDatabaseName() {
        return databaseName;
    }
    
    public int getAvailableConnections() {
        return availableConnections;
    }
    
    public static Map<String, DatabaseConnectionMultiton> getAllInstances() {
        return new ConcurrentHashMap<>(instances);
    }
}